package Frame;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import Function.FCActionLibrary;
import Function.FCCharacterLibrary;
import Function.FCEventLibrary;
import VWorldGameStudio.FCCharacter;

/**
 * 编辑或新建人物窗口
 */
public class FCEditCharacter extends JDialog {

	private JFileChooser jFileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()) {
		protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
			JDialog jDialog = super.createDialog(parent);
			jDialog.setSize(500, 400);
			jDialog.setTitle("选择文件");
			jDialog.setResizable(false);
			jDialog.setLocationRelativeTo(null);
			return jDialog;
		};
	};

	private Vector<String> actionId = new Vector<String>();
	private Vector<String> eventId = new Vector<String>();

	public FCEditCharacter(boolean isnew, FCCharacterLibrary chal, FCActionLibrary actionl, FCEventLibrary eventl) {
		setTitle((isnew) ? "人物管理-添加人物" : "人物管理-编辑人物");
		setModal(true);
		setBounds(0, 0, 415, 215);
		setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);

		JLabel jLabel = new JLabel("人物ID");
		jLabel.setBounds(10, 10, 70, 20);
		this.add(jLabel);

		JComboBox<String> jComboBox = new JComboBox<String>();
		jComboBox.setBounds(70, 10, 130, 20);
		jComboBox.setEnabled(!isnew);
		if (isnew) {
			jComboBox.addItem(String.valueOf(chal.getCharacterNewID()));
		} else {
			for(String name: chal.characterName){
				jComboBox.addItem(name);
			}
		}
		this.add(jComboBox);

		JLabel jLabel2 = new JLabel("人物名称");
		jLabel2.setBounds(210, 10, 60, 20);
		this.add(jLabel2);

		JTextField jTextField = new JTextField();
		jTextField.setBounds(270, 10, 130, 20);
		this.add(jTextField);

		JLabel jLabel3 = new JLabel("人物类型");
		jLabel3.setBounds(10, 40, 60, 20);
		this.add(jLabel3);

		JComboBox<String> jComboBox2 = new JComboBox<String>();
		jComboBox2.setBounds(70, 40, 130, 20);
		jComboBox2.addItem("player");
		jComboBox2.addItem("partner");
		jComboBox2.addItem("npc");
		jComboBox2.addItem("boss");
		this.add(jComboBox2);

		JLabel jLabel4 = new JLabel("人物行为");
		jLabel4.setBounds(210, 40, 60, 20);
		this.add(jLabel4);

		JComboBox<String> jComboBox3 = new JComboBox<String>();
		jComboBox3.setBounds(270, 40, 130, 20);
		for (int i = 0; i < actionl.getActionNum(); i++) {
			jComboBox3.addItem(actionl.getActionNameOfID(i));
		}
		this.add(jComboBox3);

		JLabel jLabel5 = new JLabel("人物事件");
		jLabel5.setBounds(10, 70, 60, 20);
		this.add(jLabel5);

		JComboBox<String> jComboBox4 = new JComboBox<String>();
		jComboBox4.setBounds(70, 70, 130, 20);
		jComboBox4.addItem("null");
		for (int i = 0; i < eventl.getEventNum(); i++)
			jComboBox4.addItem(eventl.getEventNameOfID(i));
		this.add(jComboBox4);

		JLabel jLabel6 = new JLabel("动作素材");
		jLabel6.setBounds(10, 100, 60, 20);
		this.add(jLabel6);

		JTextField jTextField2 = new JTextField();
		jTextField2.setBounds(10, 130, 190, 20);
		jTextField2.setEnabled(false);
		jTextField2.setDisabledTextColor(Color.black);
		this.add(jTextField2);

		JLabel jLabel7 = new JLabel();
		jLabel7.setBounds(260, 70, 80, 80);
		jLabel7.setBorder(BorderFactory.createLineBorder(Color.black));
		this.add(jLabel7);

		JButton jButtonSelect = new JButton("选择");
		jButtonSelect.setBounds(130, 100, 70, 20);
		this.add(jButtonSelect);

		jButtonSelect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (jFileChooser.showOpenDialog(null) == jFileChooser.APPROVE_OPTION)
					jTextField2.setText(jFileChooser.getSelectedFile().getPath().replaceAll("\\\\", "/"));
				jLabel7.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(jTextField2.getText())
						.getScaledInstance(80, 80, Image.SCALE_SMOOTH)));
			}
		});

		JButton jButtonOK = new JButton("确认");
		jButtonOK.setBounds(10, 160, 390, 18);
		this.add(jButtonOK);

		jButtonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ee) {
				if (jTextField.getText() != null && !jTextField.getText().equals("")) {
					if (jTextField2.getText() != null && !jTextField2.getText().equals("")) {
						if (!chal.isCharacterReprat(jTextField.getText()) || !isnew) {
							chal.registerCharater(isnew,
									(isnew) ? Integer.valueOf(jComboBox.getSelectedItem().toString())
											: jComboBox.getSelectedIndex(),
									jTextField.getText(), jComboBox2.getSelectedItem().toString(),
									jComboBox3.getSelectedItem().toString(), jComboBox4.getSelectedItem().toString(),
									jTextField2.getText());
							new FCTip("人物管理", "人物设置成功!");
							dispose();
						} else
							new FCTip("人物管理", "名称重复!");
					} else
						new FCTip("人物管理", "素材不得为空!");
				} else
					new FCTip("人物管理", "名称不能为空!");
			}
		});

		jComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ee) {
				FCCharacter ca = chal.getCharacterOfName(jComboBox.getSelectedItem().toString());
				jTextField.setText(ca.getCharacterName());
				jTextField.setVisible(false);
				jComboBox2.setSelectedItem(ca.getCharacterType());
				jComboBox3.setSelectedItem(ca.getCharacterActionID());
				jComboBox4.setSelectedItem(ca.getCharacterEventID());
				jTextField2.setText(ca.getCharacterPngurl());
				jLabel7.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(jTextField2.getText())
						.getScaledInstance(80, 80, Image.SCALE_SMOOTH)));
			}
		});

		setVisible(true);
	}
}
